(() => {
	let ele = {}


	ele.listDirect = $('#list-all');
	ele.listEmpty = $('#list-empty');

	ele.options = $('#tab-team li');
	ele.mores = $('.more');

	const typeList = ['', 'all', 'today', 'yestoday', 'four'];
	let typeName = 'all';
	let currentPage = {
		all: 1,
		today: 0,
		yestoday: 2,
		four: 3
	}
	tabBar();
	getFriendList(typeList.indexOf(typeName), currentPage[typeName]);
	// 选项卡选项功能
	function tabBar() {
		for (let i = 0; i < ele.options.length; i++) {
			ele.options[i].addEventListener('click', e => {
				let cls = e.target.className;
				typeName = cls.split(' ')[0].split('-')[1];
				let typeIndex = typeList.indexOf(typeName);
				if (currentPage[typeName] == 0) {
					currentPage[typeName]++;
					ele.listEmpty.style.display = 'none';
					getFriendList(typeIndex, currentPage[typeName]);
				} else if (currentPage[typeName] == 2) {
					currentPage[typeName] = 1
					getFriendList(typeIndex, currentPage[typeName])
				}
				else if (currentPage[typeName] == 3) {
					currentPage[typeName] = 1
					getFriendList(typeIndex, currentPage[typeName])
				}
				else if (currentPage[typeName] == -1) {
					ele.listEmpty.style.display = 'block';
				} else {
					ele.listEmpty.style.display = 'none';
				}
			})
		}
	}

	// 拉取好友列表
	function getFriendList(type, page) {
		console.log(type, typeName);
		let more = ele.mores[type - 1];
		more.innerText = '加载中...';
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.ORDERLOGS,
			method: 'GET',
			header: {
				'genie-token': token,
				'genie-id': userid,
			},
			data: {
				page: page,
				type: type
			},
			success: res => {
				if (res.code == 1) {
					let detail = res.data;
					if (detail.length == 0) {
						currentPage[typeName] = -1;
						more.style.display = "none";
						ele.listEmpty.style.display = "block";
					} else {
						drawFriendList(detail, res.total);
						ele.listEmpty.style.display = "none";
						page++;
						if (page > res.page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						} else {
							more.innerText = '加载更多';
							more.onclick = () => getFriendList(type, page);
						}
					}
				} else openTips(res.msg);
			}
		});
	}

	// 渲染好友列表
	function drawFriendList(data) {
		let elm = $('#list-' + typeName);
		let html = '';
		for (let i = 0; i < data.length; i++) {
			let d = data[i];
			let realsty = d.status == 3 ? 'real-status' : 'real-active';
			let txt = null
			if (d.status == 3) {
				txt = '已中奖'
			} else if (d.status == 2) {
				txt = '待开奖'
			} else if (d.status == 4) {
				txt = '未中奖'
			}
			html += '<div class="item">' +
				'<img class="header" src="../img/play/1.png" alt="">' +
				'<div class="hont">' +
				'<p class="h1">' + '甜点夺宝' + '</p>' +
				'<p class="h2">第' + 123 + '期</p>' +
				'</div>' +
				'<p class="' + realsty + '">' + txt + '</p>' +
				'</div>'
		}
		elm.innerHTML += html;
	}

})();